package cn.dankal.flyaudio.personal;

import android.widget.EditText;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import butterknife.BindView;
import butterknife.OnClick;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.protocol.PersonalProtocol;
import cn.dankal.basiclib.util.StringUtil;
import cn.dankal.basiclib.util.ToastUtils;
import cn.dankal.business.Constants;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.login.AccountCheckContract;
import cn.dankal.flyaudio.login.AccountPresenter;

/****
 * 绑定邮箱检验是否存在
 * */
@Route(path = PersonalProtocol.EDIT_EMAIL)
public class EditEmailActivity extends BaseActivity implements AccountCheckContract.View {

    private AccountPresenter mAccountPresenter;

    @BindView(R.id.et_email)
    EditText mEtEmail;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_edit_email;
    }

    @Override
    protected void initComponents() {
        addSingleTitleBar("编辑邮箱");
        mAccountPresenter = new AccountPresenter();
        mAccountPresenter.attachView(this);
        String email = getIntent().getStringExtra("email");
        if (email != null) {
            mEtEmail.setText(email);
        }
    }


    @OnClick(R.id.bt_next)
    public void onViewClicked() {
        String email = mEtEmail.getText().toString();
        if (!StringUtil.isValid(email)) {
            ToastUtils.showShort("请填写邮箱");
            return;
        }
        if (!StringUtil.checkEmail(email)) {
            ToastUtils.showShort("请输入正确的邮箱");
            return;
        }

        mAccountPresenter.checkPhoneOrEmailIsExits(Constants.CheckType.Email, email, new AccountCheckContract.CheckCallback() {
            @Override
            public void onPhoneOrEmailCheckIsExist(Constants.CheckType checkType) {
                ToastUtils.showShort("该邮箱已存在");
            }

            @Override
            public void onPhoneOrEmailCheckIsNotExist(Constants.CheckType checkType) {
                ARouter.getInstance().build(PersonalProtocol.MODIFY_EMAIL)
                        .withString("email", email)
                        .navigation();
                finish();
            }

            @Override
            public void onPhoneOrEmailCheckError(String errorInfo) {
                ToastUtils.showShort(errorInfo);
            }
        });
    }

}
